package com.example.rabbitdemo.services;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class RabbitMqSenderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 消息发送者
     *
     * @param exchange 交换机
     * @param routingKey 路由键值
     * @param message 消息信息
     * @return
     */
    public void send(String exchange, String routingKey, Message message) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        System.out.println("开始发送消息 : " + message);
        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationId);
        System.out.println("结束发送消息 : " + message);
    }


    public void sendQueue(String exchange, String routingKey,Message message){
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
    }
}
